package com.ecoolex.ying.space.core.manager;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.pojo.Sortings;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.ying.space.core.dao.EstateProjectActivityHouseModelDAO;
import com.ecoolex.ying.space.entity.EstateProjectActivity;
import com.ecoolex.ying.space.entity.EstateProjectActivityHouseModel;
import com.ecoolex.ying.space.entity.EstateProjectActivityHouseModel.Column;
import com.ecoolex.ying.space.entity.EstateProjectActivityHouseModelExample;
import com.ecoolex.ying.space.entity.EstateProjectActivityHouseModelExample.Criteria;
import com.ecoolex.ying.space.pojo.AddEstateProjectActivityHouseModelItemsDTO;

import java.time.Instant;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class EstateProjectActivityHouseModelManager {
	
    @Autowired
    private EstateProjectActivityHouseModelDAO dao;
    
    @Autowired
    private EstateProjectActivityHouseModelItemsManager itemsManager;
    
    @Autowired
    private EstateProjectActivityManager activityManager;
    
    private void verify(EstateProjectActivityHouseModel record) {
    	BizAssert.isTrue(Check.notNullOrEmpty(record.getName()),ResultCode.VERIFY.build(10), "方案名称为空");
    	BizAssert.isTrue(Check.notNull(record.getProjectId()),ResultCode.VERIFY.build(11), "项目Id为空");
    	BizAssert.isTrue(Check.notNullOrEmpty(record.getCoverImage()),ResultCode.VERIFY.build(12), "方案封面图为空");
    	EstateProjectActivity estateProjectActivity = activityManager.get(record.getProjectId());
    	BizAssert.isTrue(Check.notNull(estateProjectActivity),ResultCode.VERIFY.build(14), "所属项目projectId={}不存在",record.getProjectId());
    }
    
    @Transactional
    public EstateProjectActivityHouseModel add(EstateProjectActivityHouseModel record, List<AddEstateProjectActivityHouseModelItemsDTO> itemsDTOList, String createUser) {
    	verify(record);
    	record.setDeleted(EstateProjectActivityHouseModel.NOT_DELETED);
		record.setCreateUser(createUser);
		record.setCreateTime(Date.from(Instant.now()));
		record.setUpdateUser(createUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.insert(record);
		//处理户型方案条目
		itemsManager.edit(record.getModelId(), itemsDTOList, createUser);
		return record;
    }
    
    @Transactional
    public EstateProjectActivityHouseModel edit(EstateProjectActivityHouseModel record,List<AddEstateProjectActivityHouseModelItemsDTO> itemsDTOList, String updateUser) {
    	verify(record);
    	record.setUpdateUser(updateUser);
    	record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKey(record);
		//处理户型方案条目
		itemsManager.edit(record.getModelId(), itemsDTOList, updateUser);
		return record;
    }
    
    public void delete(EstateProjectActivityHouseModel model,String updateUser) {
    	model.setDeleted(EstateProjectActivityHouseModel.IS_DELETED);
    	model.setUpdateUser(updateUser);
    	model.setUpdateTime(Date.from(Instant.now()));
    	dao.updateByPrimaryKey(model);
    }
    
    public List<EstateProjectActivityHouseModel> listByProjectId(Integer projectId){
    	EstateProjectActivityHouseModelExample example = new EstateProjectActivityHouseModelExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andProjectIdEqualTo(projectId).andDeletedEqualTo(EstateProjectActivityHouseModel.NOT_DELETED);
    	return dao.selectByExample(example);
    }
    
    public EstateProjectActivityHouseModel get(Integer modelId) {
    	return dao.selectByPrimaryKeyWithLogicalDelete(modelId, EstateProjectActivityHouseModel.NOT_DELETED);
    }
    
    public Pagination<EstateProjectActivityHouseModel> modelList(Integer projectId,Page page) {
    	EstateProjectActivityHouseModelExample example = new EstateProjectActivityHouseModelExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andProjectIdEqualTo(projectId);
    	criteria.andDeletedEqualTo(EstateProjectActivityHouseModel.NOT_DELETED);
    	example.setOffset(page.getFirstIndex());
    	example.setRows(page.getPageSize());
    	example.orderBy(Column.createTime.desc());
		return Pagination.create(page, 
			dao.countByExample(example),
			() -> dao.selectByExample(example));
	}
    
	public Pagination<EstateProjectActivityHouseModel> modelList(Integer projectId, Sorting[] sorts, Page page) {
		EstateProjectActivityHouseModelExample example = new EstateProjectActivityHouseModelExample();
    	Criteria criteria = example.createCriteria();
    	if(Check.notNull(projectId)) {
    		criteria.andProjectIdEqualTo(projectId);
    	}
    	criteria.andDeletedEqualTo(EstateProjectActivityHouseModel.NOT_DELETED);
    	example.setOffset(page.getFirstIndex());
    	example.setRows(page.getPageSize());
    	example.orderBy(Sortings.builder(sorts).verify(Column::valueOf).toSql());
		return Pagination.create(page, 
			dao.countByExample(example),
			() -> dao.selectByExample(example));
	}
}